clear all
set memory 500m
set matsize 1000
set more off
cd "C:\Users\Maryam\Desktop\BS Studies\Revisions\Data Set Kuntal Stata"


***************************************************************************************************
**********Classifying sectors to create employment variables with ilo data*************
*********This is needed to create the productivity variable later*******************
***************************************************************************************************

use ilo1970_2008.dta, clear

sort countryorarea year
rename countryorarea country
drop sourceid valuefootnotes
tab country
keep if country=="Hong Kong, China" | country=="Indonesia" | country=="Korea, Republic of" | country=="Malaysia" | country=="Pakistan" | ///
country=="Philippines" | country=="Singapore" | country=="Sri Lanka" | country=="Thailand" | country=="United States"
keep if sex=="Total men and women"
drop sex
keep if source =="Labour force survey"
drop source
drop if classification=="ISIC-Rev.4"

sort country year classification subclassification

replace subclassification = "isic2_t" if subclassification=="Total." & classification=="ISIC-Rev.2"
replace subclassification = "isic3_t" if subclassification=="Total." & classification=="ISIC-Rev.3"

gen subcode = "isic2_0" if subclassification == "0. Activities not Adequately Defined"
replace subcode = "isic2_1" if subclassification == "1. Agriculture, Hunting, Forestry and Fishing"
replace subcode = "isic2_2" if subclassification == "2. Mining and Quarrying"
replace subcode = "isic2_3" if subclassification == "3. Manufacturing"
replace subcode = "isic2_4" if subclassification == "4. Electricity, Gas and Water"
replace subcode = "isic2_5" if subclassification == "5. Construction"
replace subcode = "isic2_6" if subclassification == "6. Wholesale and Retail Trade and Restaurants and Hotels"
replace subcode = "isic2_7" if subclassification == "7. Transport, Storage and Communication"
replace subcode = "isic2_8" if subclassification == "8. Financing, Insurance, Real Estate and Business Services"
replace subcode = "isic2_9" if subclassification == "9. Community, Social and Personal Services"
replace subcode = "isic3_ab" if subclassification == "A-B."
replace subcode = "isic3_acex" if subclassification == "A-C,E,X."
replace subcode = "isic3_a" if subclassification == "A. Agriculture, Hunting and Forestry"
replace subcode = "isic3_b" if subclassification == "B. Fishing"
replace subcode = "isic3_ce" if subclassification == "C,E."
replace subcode = "isic3_c" if subclassification == "C. Mining and Quarrying"
replace subcode = "isic3_d" if subclassification == "D. Manufacturing"
replace subcode = "isic3_e" if subclassification == "E. Electricity, Gas and Water Supply"
replace subcode = "isic3_f" if subclassification == "F. Construction"
replace subcode = "isic3_gh" if subclassification == "G-H."
replace subcode = "isic3_g" if subclassification == "G. Wholesale and Retail Trade; Repair of Motor Vehicles, Motorcycles and Personal and Household Goods"
replace subcode = "isic3_h" if subclassification == "H. Hotels and Restaurants"
replace subcode = "isic3_i" if subclassification == "I. Transport, Storage and Communications"
replace subcode = "isic3_jk" if subclassification == "J-K."
replace subcode = "isic3_j" if subclassification == "J. Financial Intermediation"
replace subcode = "isic3_k" if subclassification == "K. Real Estate, Renting and Business Activities"
replace subcode = "isic3_lm" if subclassification == "L,M."
replace subcode = "isic3_lq" if subclassification == "L-Q."
replace subcode = "isic3_l" if subclassification == "L. Public Administration and Defence; Compulsory Social Security"
replace subcode = "isic3_mn" if subclassification == "M-N."
replace subcode = "isic3_m" if subclassification == "M. Education"
replace subcode = "isic3_n" if subclassification == "N. Health and Social Work"
replace subcode = "isic3_oq" if subclassification == "O,Q."
replace subcode = "isic3_op" if subclassification == "O-P."
replace subcode = "isic3_o_q" if subclassification == "O-Q."
replace subcode = "isic3_ox" if subclassification == "O-X."
replace subcode = "isic3_o" if subclassification == "O. Other Community,Social and Personal Service Activities"
replace subcode = "isic3_p" if subclassification == "P. Households with Employed Persons"
replace subcode = "isic3_qx" if subclassification == "Q-X."
replace subcode = "isic3_q" if subclassification == "Q. Extra-Territorial Organizations and Bodies"
replace subcode = "isic3_x" if subclassification == "X. Not classifiable by economic activity"

replace subcode = "isic2_t" if subclassification == "isic2_t"
replace subcode = "isic3_t" if subclassification == "isic3_t"

save ilo1970_2008_1.dta, replace


*****************************************************
***United States***
*****************************************************

clear
use ilo1970_2008_1.dta, replace
keep if country=="United States"
save UnitedStates.dta, replace
drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_1
rename value2 isic2_2
rename value3 isic2_3
rename value4 isic2_4
rename value5 isic2_5
rename value6 isic2_6
rename value7 isic2_7
rename value8 isic2_8
rename value9 isic2_9
rename value10 isic2_t
rename value11 isic3_ab
rename value12 isic3_c
rename value13 isic3_d
rename value14 isic3_e
rename value15 isic3_f
rename value16 isic3_g
rename value17 isic3_h
rename value18 isic3_i
rename value19 isic3_j
rename value20 isic3_k
rename value21 isic3_l
rename value22 isic3_m
rename value23 isic3_n
rename value24 isic3_ox
rename value25 isic3_t

gen country = "United States"
order country year
save UnitedStates_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic3" if isic3_t != .
replace isic = "isic2" if isic == "isic"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9) if isic == "isic2"

gen agr3 = isic3_ab if isic == "isic3"
replace agr = agr3 if isic == "isic3"

gen min3 = (isic3_c + isic3_e) if isic == "isic3"
replace min = min3 if isic == "isic3"

gen man3 = isic3_d if isic == "isic3"
replace man = man3 if isic == "isic3"

gen con3 = isic3_f if isic == "isic3"
replace con = con3 if isic == "isic3"

gen who3 = (isic3_g + isic3_h) if isic == "isic3"
replace who = who3 if isic == "isic3"

gen tra3 = isic3_i if isic == "isic3"
replace tra = tra3 if isic == "isic3"

gen oth3 = (isic3_j + isic3_k + isic3_l + isic3_m + isic3_n + isic3_ox) if isic == "isic3"
replace oth = oth3 if isic == "isic3"


save UnitedStates_2.dta, replace


*****************************************************
***Hong Kong***
*****************************************************


clear
use ilo1970_2008_1.dta, replace
keep if country=="Hong Kong, China"
save HongKong.dta, replace
drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_0
rename value2 isic2_1
rename value3 isic2_2
rename value4 isic2_3
rename value5 isic2_4
rename value6 isic2_5
rename value7 isic2_6
rename value8 isic2_7
rename value9 isic2_8
rename value10 isic2_9
rename value11 isic2_t
gen country = "Hong Kong"
order country year
save HongKong_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic2" if isic2_t != .
replace isic = "isic3" if isic == "isic"

replace isic2_0 = 0 if isic2_0 == . & isic == "isic2"
replace isic2_2 = isic2_t - (isic2_0 + isic2_1 + isic2_3 + isic2_4 + isic2_5 + isic2_6 + isic2_7 + isic2_8 + isic2_9) if isic2_2 == . & isic == "isic2"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9 + isic2_0) if isic == "isic2"

save HongKong_2.dta, replace


*****************************************************
***Indonesia***
*****************************************************


clear
use ilo1970_2008_1.dta, replace
keep if country=="Indonesia"
save Indonesia.dta, replace
drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_0
rename value2 isic2_1
rename value3 isic2_2
rename value4 isic2_3
rename value5 isic2_4
rename value6 isic2_5
rename value7 isic2_6
rename value8 isic2_7
rename value9 isic2_8
rename value10 isic2_9
rename value11 isic2_t
rename value12 isic3_a
rename value13 isic3_b
rename value14 isic3_c
rename value15 isic3_d
rename value16 isic3_e
rename value17 isic3_f
rename value18 isic3_g
rename value19 isic3_h
rename value20 isic3_i
rename value21 isic3_j
rename value22 isic3_k
rename value23 isic3_l
rename value24 isic3_m
rename value25 isic3_n
rename value26 isic3_o
rename value27 isic3_p
rename value28 isic3_q
rename value29 isic3_t
rename value30 isic3_x
gen country = "Indonesia"
order country year
save Indonesia_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic3" if isic3_t != .
replace isic = "isic2" if isic == "isic"

replace isic2_0 = isic2_t - (isic2_1 + isic2_2 + isic2_3 + isic2_4 + isic2_5 + isic2_6 + isic2_7 + isic2_8 + isic2_9) if isic2_0 == . & isic == "isic2"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9 + isic2_0) if isic == "isic2"

gen agr3 = (isic3_a + isic3_b) if isic == "isic3"
replace agr = agr3 if isic == "isic3"

gen min3 = (isic3_c + isic3_e) if isic == "isic3"
replace min = min3 if isic == "isic3"

gen man3 = isic3_d if isic == "isic3"
replace man = man3 if isic == "isic3"

gen con3 = isic3_f if isic == "isic3"
replace con = con3 if isic == "isic3"

gen who3 = (isic3_g + isic3_h) if isic == "isic3"
replace who = who3 if isic == "isic3"

gen tra3 = isic3_i if isic == "isic3"
replace tra = tra3 if isic == "isic3"

gen oth3 = (isic3_j + isic3_k + isic3_l + isic3_m + isic3_n + isic3_o + isic3_p + isic3_q + isic3_x) if isic == "isic3"
replace oth = oth3 if isic == "isic3"

save Indonesia_2.dta, replace


*****************************************************
***Korea***
*****************************************************

clear
use ilo1970_2008_1.dta, replace
keep if country=="Korea, Republic of"
save Korea.dta, replace

drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_1
rename value2 isic2_2
rename value3 isic2_3
rename value4 isic2_4
rename value5 isic2_5
rename value6 isic2_6
rename value7 isic2_7
rename value8 isic2_8
rename value9 isic2_9
rename value10 isic2_t
rename value11 isic3_a
rename value12 isic3_b
rename value13 isic3_c
rename value14 isic3_d
rename value15 isic3_e
rename value16 isic3_f
rename value17 isic3_g
rename value18 isic3_h
rename value19 isic3_i
rename value20 isic3_j
rename value21 isic3_k
rename value22 isic3_l
rename value23 isic3_m
rename value24 isic3_n
rename value25 isic3_o
rename value26 isic3_p
rename value27 isic3_q
rename value28 isic3_t

gen country = "Korea"
order country year
save Korea_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic3" if isic3_t != .
replace isic = "isic2" if isic == "isic"

replace isic2_8 = isic2_t - (isic2_1 + isic2_2 + isic2_3 + isic2_4 + isic2_5 + isic2_6 + isic2_7 + isic2_9) if isic2_8 == . & isic == "isic2"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9) if isic == "isic2"

gen agr3 = (isic3_a + isic3_b) if isic == "isic3"
replace agr = agr3 if isic == "isic3"

gen min3 = (isic3_c + isic3_e) if isic == "isic3"
replace min = min3 if isic == "isic3"

gen man3 = isic3_d if isic == "isic3"
replace man = man3 if isic == "isic3"

gen con3 = isic3_f if isic == "isic3"
replace con = con3 if isic == "isic3"

gen who3 = (isic3_g + isic3_h) if isic == "isic3"
replace who = who3 if isic == "isic3"

gen tra3 = isic3_i if isic == "isic3"
replace tra = tra3 if isic == "isic3"

gen oth3 = (isic3_j + isic3_k + isic3_l + isic3_m + isic3_n + isic3_o + isic3_p + isic3_q) if isic == "isic3"
replace oth = oth3 if isic == "isic3"

save Korea_2.dta, replace


*****************************************************
***Malaysia***
*****************************************************


clear
use ilo1970_2008_1.dta, replace
keep if country=="Malaysia"
save Malaysia.dta, replace
drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_0
rename value2 isic2_1
rename value3 isic2_2
rename value4 isic2_3
rename value5 isic2_4
rename value6 isic2_5
rename value7 isic2_6
rename value8 isic2_7
rename value9 isic2_8
rename value10 isic2_9
rename value11 isic2_t
rename value12 isic3_a
rename value13 isic3_b
rename value14 isic3_c
rename value15 isic3_d
rename value16 isic3_e
rename value17 isic3_f
rename value18 isic3_g
rename value19 isic3_h
rename value20 isic3_i
rename value21 isic3_j
rename value22 isic3_k
rename value23 isic3_l
rename value24 isic3_m
rename value25 isic3_n
rename value26 isic3_o
rename value27 isic3_p
rename value28 isic3_q
rename value29 isic3_t


gen country = "Malaysia"
order country year
save Malaysia_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic3" if isic3_t != .
replace isic = "isic2" if isic == "isic"

replace isic2_8 = isic2_t - (isic2_0  + isic2_1 + isic2_2 + isic2_3 + isic2_4 + isic2_5 + isic2_6 + isic2_7 + isic2_9) if isic2_8 == . & isic == "isic2"
replace isic2_0 = isic2_t - (isic2_1 + isic2_2 + isic2_3 + isic2_4 + isic2_5 + isic2_6 + isic2_7 + isic2_8  + isic2_9) if isic2_0 == . & isic == "isic2"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9 + isic2_0) if isic == "isic2"

gen agr3 = (isic3_a + isic3_b) if isic == "isic3"
replace agr = agr3 if isic == "isic3"

gen min3 = (isic3_c + isic3_e) if isic == "isic3"
replace min = min3 if isic == "isic3"

gen man3 = isic3_d if isic == "isic3"
replace man = man3 if isic == "isic3"

gen con3 = isic3_f if isic == "isic3"
replace con = con3 if isic == "isic3"

gen who3 = (isic3_g + isic3_h) if isic == "isic3"
replace who = who3 if isic == "isic3"

gen tra3 = isic3_i if isic == "isic3"
replace tra = tra3 if isic == "isic3"

gen oth3 = (isic3_j + isic3_k + isic3_l + isic3_m + isic3_n + isic3_o + isic3_p + isic3_q) if isic == "isic3"
replace oth = oth3 if isic == "isic3"

save Malaysia_2.dta, replace


*****************************************************
***Pakistan***
*****************************************************


clear
use ilo1970_2008_1.dta, replace
keep if country=="Pakistan"
save Pakistan.dta, replace
drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_0
rename value2 isic2_1
rename value3 isic2_2
rename value4 isic2_3
rename value5 isic2_4
rename value6 isic2_5
rename value7 isic2_6
rename value8 isic2_7
rename value9 isic2_8
rename value10 isic2_9
rename value11 isic2_t

gen country = "Pakistan"
order country year
save Pakistan_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic2" if isic2_t != .
replace isic = "isic3" if isic == "isic"

replace isic2_0 = isic2_t - (isic2_1 + isic2_2 + isic2_3 + isic2_4 + isic2_5 + isic2_6 + isic2_7 + isic2_8  + isic2_9) if isic2_0 == . & isic == "isic2"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9 + isic2_0) if isic == "isic2"

save Pakistan_2.dta, replace


*****************************************************
***Philippines***
*****************************************************


clear
use ilo1970_2008_1.dta, replace
keep if country=="Philippines"
save Philippines.dta, replace
drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_0
rename value2 isic2_1
rename value3 isic2_2
rename value4 isic2_3
rename value5 isic2_4
rename value6 isic2_5
rename value7 isic2_6
rename value8 isic2_7
rename value9 isic2_8
rename value10 isic2_9
rename value11 isic2_t
rename value12 isic3_a
rename value13 isic3_b
rename value14 isic3_c
rename value15 isic3_d
rename value16 isic3_e
rename value17 isic3_f
rename value18 isic3_g
rename value19 isic3_h
rename value20 isic3_i
rename value21 isic3_j
rename value22 isic3_k
rename value23 isic3_l
rename value24 isic3_m
rename value25 isic3_n
rename value26 isic3_o
rename value27 isic3_p
rename value28 isic3_q
rename value29 isic3_t

gen country = "Philippines"
order country year
save Philippines_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic3" if isic3_t != .
replace isic = "isic2" if isic == "isic"

replace isic2_8 = isic2_t - (isic2_0  + isic2_1 + isic2_2 + isic2_3 + isic2_4 + isic2_5 + isic2_6 + isic2_7 + isic2_9) if isic2_8 == . & isic == "isic2"
replace isic2_8 = 0 if isic2_8 < 0
replace isic2_0 = isic2_t - (isic2_1 + isic2_2 + isic2_3 + isic2_4 + isic2_5 + isic2_6 + isic2_7 + isic2_8  + isic2_9) if isic2_0 == . & isic == "isic2"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9 + isic2_0) if isic == "isic2"

gen agr3 = (isic3_a + isic3_b) if isic == "isic3"
replace agr = agr3 if isic == "isic3"

gen min3 = (isic3_c + isic3_e) if isic == "isic3"
replace min = min3 if isic == "isic3"

gen man3 = isic3_d if isic == "isic3"
replace man = man3 if isic == "isic3"

gen con3 = isic3_f if isic == "isic3"
replace con = con3 if isic == "isic3"

gen who3 = (isic3_g + isic3_h) if isic == "isic3"
replace who = who3 if isic == "isic3"

gen tra3 = isic3_i if isic == "isic3"
replace tra = tra3 if isic == "isic3"

gen oth3 = (isic3_j + isic3_k + isic3_l + isic3_m + isic3_n + isic3_o + isic3_p + isic3_q) if isic == "isic3"
replace oth = oth3 if isic == "isic3"

save Philippines_2.dta, replace

*********************************************************************************************************************
***Thailand***
***No Seperate Financial (Sector 8) in ISIC2 Classification. Sectors 8 and 9 are reported in a combined way***********
***********************************************************************************************************************

clear
use ilo1970_2008_1.dta, replace
keep if country=="Thailand"
save Thailand.dta, replace
drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_0
rename value2 isic2_1
rename value3 isic2_2
rename value4 isic2_3
rename value5 isic2_4
rename value6 isic2_5
rename value7 isic2_6
rename value8 isic2_7
rename value9 isic2_9
rename value10 isic2_t
rename value11 isic3_a
rename value12 isic3_b
rename value13 isic3_c
rename value14 isic3_d
rename value15 isic3_e
rename value16 isic3_f
rename value17 isic3_g
rename value18 isic3_h
rename value19 isic3_i
rename value20 isic3_j
rename value21 isic3_k
rename value22 isic3_l
rename value23 isic3_m
rename value24 isic3_n
rename value25 isic3_o
rename value26 isic3_p
rename value27 isic3_q
rename value28 isic3_t
rename value29 isic3_x


gen country = "Thailand"
order country year
save Thailand_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic3" if isic3_t != .
replace isic = "isic2" if isic == "isic"

replace isic2_0 = isic2_t - (isic2_1 + isic2_2 + isic2_3 + isic2_4 + isic2_5 + isic2_6 + isic2_7 + isic2_9) if isic2_0 == . & isic == "isic2"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_9 + isic2_0) if isic == "isic2"

gen agr3 = (isic3_a + isic3_b) if isic == "isic3"
replace agr = agr3 if isic == "isic3"

gen min3 = (isic3_c + isic3_e) if isic == "isic3"
replace min = min3 if isic == "isic3"

gen man3 = isic3_d if isic == "isic3"
replace man = man3 if isic == "isic3"

gen con3 = isic3_f if isic == "isic3"
replace con = con3 if isic == "isic3"

gen who3 = (isic3_g + isic3_h) if isic == "isic3"
replace who = who3 if isic == "isic3"

gen tra3 = isic3_i if isic == "isic3"
replace tra = tra3 if isic == "isic3"

gen oth3 = (isic3_l + isic3_m + isic3_n + isic3_o + isic3_p + isic3_q + isic3_x) if isic == "isic3"
replace oth = oth3 if isic == "isic3"

save Thailand_2.dta, replace


***************************************************************************************************
***Singapore***
***Since 1997, Agr + Min + Oth sectors are all merged. Since all are traded, no problem***
***Accordingly compare with US (agrminoth) sector*************
***************************************************************************************************


clear
use ilo1970_2008_1.dta, replace
keep if country=="Singapore"
save Singapore.dta, replace
drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_0
rename value2 isic2_1
rename value3 isic2_2
rename value4 isic2_3
rename value5 isic2_4
rename value6 isic2_5
rename value7 isic2_6
rename value8 isic2_7
rename value9 isic2_8
rename value10 isic2_9
rename value11 isic2_t
rename value12 isic3_ab
rename value13 isic3_acex
rename value14 isic3_c
rename value15 isic3_d
rename value16 isic3_e
rename value17 isic3_f
rename value18 isic3_g
rename value19 isic3_gh
rename value20 isic3_h
rename value21 isic3_i
rename value22 isic3_j
rename value23 isic3_jk
rename value24 isic3_k
rename value25 isic3_l
rename value26 isic3_lm
rename value27 isic3_lq
rename value28 isic3_mn
rename value29 isic3_n
rename value30 isic3_o_q
rename value31 isic3_op
rename value32 isic3_qx
rename value33 isic3_t

gen country = "Singapore"
order country year
save Singapore_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic3" if isic3_t != .
replace isic = "isic2" if isic == "isic"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9 + isic2_0) if isic == "isic2"

gen agr3 = isic3_ab if isic == "isic3"
replace agr = agr3 if isic == "isic3"

gen min3 = (isic3_c + isic3_e) if isic == "isic3"
replace min = min3 if isic == "isic3"

gen man3 = isic3_d if isic == "isic3"
replace man = man3 if isic == "isic3"

gen con3 = isic3_f if isic == "isic3"
replace con = con3 if isic == "isic3"

gen who3 = (isic3_g + isic3_h) if isic == "isic3"
replace who3 = isic3_gh if who3 ==. & isic == "isic3"
replace who = who3 if isic == "isic3"

gen tra3 = isic3_i if isic == "isic3"
replace tra = tra3 if isic == "isic3"

gen oth31 = (isic3_j + isic3_k + isic3_l + isic3_mn + isic3_op + isic3_qx) if isic == "isic3"
gen oth32 = (isic3_jk + isic3_lq) if isic == "isic3"
gen oth33 = (isic3_j + isic3_k + isic3_lm + isic3_n + isic3_o_q) if isic == "isic3"

gen oth3 = oth31 if isic == "isic3"
replace oth3 = oth32 if oth3 == . & isic == "isic3"
replace oth3 = oth33 if oth3 == . & isic == "isic3"
replace oth = oth3 if isic == "isic3"

gen agrminoth = (isic3_acex + isic3_j + isic3_k + isic3_lm + isic3_n + isic3_o_q) if isic == "isic3" & year >= 1997

save Singapore_2.dta, replace


***************************************************************************************************
***Sri Lanka***
***No Construction Sector in ISIC3-ISIC4 Classification. Min and Cons are merged from 2002. ***
***Since Con has a greater weight and Con is non-traded, use the merged sector as nontraded.***
***************************************************************************************************


clear
use ilo1970_2008_1.dta, replace
keep if country=="Sri Lanka"
save SriLanka.dta, replace
drop country classification subclassification
encode subcode, gen(code)
drop subcode
order year code
reshape wide value, i(year) j(code)

rename value1 isic2_0
rename value2 isic2_1
rename value3 isic2_2
rename value4 isic2_3
rename value5 isic2_4
rename value6 isic2_5
rename value7 isic2_6
rename value8 isic2_7
rename value9 isic2_8
rename value10 isic2_9
rename value11 isic2_t
rename value12 isic3_ab
rename value13 isic3_ce
rename value14 isic3_d
rename value15 isic3_g
rename value16 isic3_h
rename value17 isic3_i
rename value18 isic3_jk
rename value19 isic3_l
rename value20 isic3_m
rename value21 isic3_n
rename value22 isic3_oq
rename value23 isic3_p
rename value24 isic3_t
rename value25 isic3_x

gen country = "Sri Lanka"
order country year
save SriLanka_1.dta, replace


gen isic = "isic"
order country year isic
replace isic = "isic3" if isic3_t != .
replace isic = "isic2" if isic == "isic"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9 + isic2_0) if isic == "isic2"

gen agr3 = isic3_ab if isic == "isic3"
replace agr = agr3 if isic == "isic3"

gen min3 = isic3_ce if isic == "isic3"
replace min = min3 if isic == "isic3"

gen man3 = isic3_d if isic == "isic3"
replace man = man3 if isic == "isic3"

gen who3 = (isic3_g + isic3_h) if isic == "isic3"
replace who = who3 if isic == "isic3"

gen tra3 = isic3_i if isic == "isic3"
replace tra = tra3 if isic == "isic3"

gen oth3 = (isic3_jk + isic3_l + isic3_m + isic3_n + isic3_oq + isic3_p + isic3_x) if isic == "isic3"
replace oth = oth3 if isic == "isic3"

save SriLanka_2.dta, replace


***************************************************************************************************
***Japan***
***Can't use Japan as sectors p and q are missing***
***************************************************************************************************


clear
use Japan_1.dta, replace

gen isic = "isic"
order country year isic
replace isic = "isic3" if isic3_t != .
replace isic = "isic2" if isic == "isic"

gen agr = isic2_1 if isic == "isic2"
gen min = (isic2_2 + isic2_4) if isic == "isic2"
gen man = isic2_3 if isic == "isic2"
gen con = isic2_5 if isic == "isic2"
gen who = isic2_6 if isic == "isic2"
gen tra = isic2_7 if isic == "isic2"
gen oth = (isic2_8 + isic2_9 + isic2_0) if isic == "isic2"

gen agr3 = (isic3_a + isic3_b) if isic == "isic3"
replace agr = agr3 if isic == "isic3"

gen min3 = (isic3_c + isic3_e) if isic == "isic3"
replace min = min3 if isic == "isic3"

gen man3 = isic3_d if isic == "isic3"
replace man = man3 if isic == "isic3"

gen con3 = isic3_f if isic == "isic3"
replace con = con3 if isic == "isic3"

gen who3 = (isic3_g + isic3_h) if isic == "isic3"
replace who = who3 if isic == "isic3"

gen tra3 = isic3_i if isic == "isic3"
replace tra = tra3 if isic == "isic3"

gen oth3 = (isic3_j + isic3_k + isic3_l + isic3_m + isic3_n + isic3_o + isic3_x) if isic == "isic3"
replace oth = oth3 if isic == "isic3"

save Japan_2.dta, replace



***************************************************************************************************
**********Merging all the country data for employment from ILO*************
***************************************************************************************************


clear
use HongKong_2.dta, replace
sort country year
save HongKong_2.dta, replace

clear
use Indonesia_2.dta, replace
sort country year
save Indonesia_2.dta, replace

clear
use Japan_2.dta, replace
sort country year
save Japan_2.dta, replace

clear
use Korea_2.dta, replace
sort country year
save Korea_2.dta, replace

clear
use Malaysia_2.dta, replace
sort country year
save Malaysia_2.dta, replace

clear
use Philippines_2.dta, replace
sort country year
save Philippines_2.dta, replace

clear
use Pakistan_2.dta, replace
sort country year
save Pakistan_2.dta, replace

clear
use SriLanka_2.dta, replace
sort country year
save SriLanka_2.dta, replace

clear
use Thailand_2.dta, replace
sort country year
save Thailand_2.dta, replace

clear
use Singapore_2.dta, replace
sort country year
save Singapore_2.dta, replace

clear
use UnitedStates_2.dta, replace
sort country year
save UnitedStates_2.dta, replace

merge country year using HongKong_2.dta Indonesia_2.dta Japan_2.dta Korea_2.dta Malaysia_2.dta Philippines_2.dta ///
Pakistan_2.dta SriLanka_2.dta Thailand_2.dta Singapore_2.dta UnitedStates_2.dta

drop _merge _merge11 _merge10 _merge9 _merge8 _merge7 _merge6 _merge5 _merge4 _merge3 _merge2 _merge1

order country year isic isic2_0 isic2_1 isic2_2 isic2_3 isic2_4 isic2_5 isic2_6 isic2_7 isic2_8 isic2_9 isic2_t isic3_a isic3_b isic3_ab isic3_c ///
isic3_d isic3_e isic3_ce isic3_f isic3_g isic3_h isic3_gh isic3_i isic3_j isic3_k isic3_jk isic3_l isic3_m isic3_lm isic3_n isic3_mn isic3_o isic3_p ///
isic3_op isic3_q isic3_lq isic3_oq isic3_o_q isic3_x isic3_ox isic3_qx isic3_t

sort country year

label var isic2_0 "0. Activities not Adequately Defined"
label var isic2_1 "1. Agriculture, Hunting, Forestry and Fishing"
label var isic2_2 "2. Mining and Quarrying"
label var isic2_3 "3. Manufacturing"
label var isic2_4 "4. Electricity, Gas and Water"
label var isic2_5 "5. Construction"
label var isic2_6 "6. Wholesale and Retail Trade and Restaurants and Hotels"
label var isic2_7 "7. Transport, Storage and Communication"
label var isic2_8 "8. Financing, Insurance, Real Estate and Business Services"
label var isic2_9 "9. Community, Social and Personal Services"
label var isic3_ab "A-B."
label var isic3_acex "A-C,E,X."
label var isic3_a "A. Agriculture, Hunting and Forestry"
label var isic3_b "B. Fishing"
label var isic3_ce "C,E."
label var isic3_c "C. Mining and Quarrying"
label var isic3_d "D. Manufacturing"
label var isic3_e "E. Electricity, Gas and Water Supply"
label var isic3_f "F. Construction"
label var isic3_gh "G-H."
label var isic3_g "G. Wholesale and Retail Trade; Repair of Motor Vehicles, Motorcycles and Personal and Household Goods"
label var isic3_h "H. Hotels and Restaurants"
label var isic3_i "I. Transport, Storage and Communications"
label var isic3_jk "J-K."
label var isic3_j "J. Financial Intermediation"
label var isic3_k "K. Real Estate, Renting and Business Activities"
label var isic3_lm "L,M."
label var isic3_lq "L-Q."
label var isic3_l "L. Public Administration and Defence; Compulsory Social Security"
label var isic3_mn "M-N."
label var isic3_m "M. Education"
label var isic3_n "N. Health and Social Work"
label var isic3_oq "O,Q."
label var isic3_op "O-P."
label var isic3_o_q "O-Q."
label var isic3_ox "O-X."
label var isic3_o "O. Other Community,Social and Personal Service Activities"
label var isic3_p "P. Households with Employed Persons"
label var isic3_qx "Q-X."
label var isic3_q "Q. Extra-Territorial Organizations and Bodies"
label var isic3_x "X. Not classifiable by economic activity"
label var isic2_t "isic2_Total"
label var isic3_t "isic3_Total"


save panel_1.dta, replace


keep country year isic agr min man con who tra oth agrminoth
sort country year
save panel_1a.dta, replace


*******************Dropping years to merge with more recent employment data from another file (ILO)*******************


drop if country=="Hong Kong" & year>1997
drop if country=="Indonesia" & year>1999
drop if country=="Japan" & year>1997
drop if country=="Korea" & year>1997
drop if country=="Malaysia" & year>1997
drop if country=="Pakistan" & year>1997
drop if country=="Philippines" & year>1997
drop if country=="Sri Lanka" & year>2001
drop if country=="Thailand" & year>1997

sort country year
save panel_1b.dta, replace


**************Creating this file to generate missing years withing panel**********************
**************Starting years for each country varies with data availability*******************


clear
use country.dta, replace
sort country year
save country.dta, replace

clear
use ilo2009_2013.dta,replace
sort country year

merge country year using panel_1b.dta country.dta
drop _merge _merge1 _merge2

replace isic = "isic4" if isic == ""
sort country year
order country isic

replace agrminoth = (agr + min + oth) if agrminoth==. & country=="Singapore"

gen mincon = .
replace mincon = min + con if country=="Sri Lanka"
replace mincon = min if mincon==. & country=="Sri Lanka"


********************Interpolating data to fill in missing observations**************************
********************Note: Does not fill inall years in the gap between years is too large*******

by country: ipolate agr year, gen(iagr)
by country: ipolate min year, gen(imin)
by country: ipolate man year, gen(iman)
by country: ipolate con year, gen(icon)
by country: ipolate who year, gen(iwho)
by country: ipolate tra year, gen(itra)
by country: ipolate oth year, gen(ioth)
by country: ipolate agrminoth year, gen(iagrminoth)

sort country year

save panel_2.dta, replace


***************************************************************************************************
*************Create VA variables with UNCTAD data with current and constant VA data******************
*************Note: Constant VA variables in UNCTAD are with constant 2005 USD. This needs to be adjusted for Real VA. Not done in this section.******************
***************************************************************************************************


clear
set more off

use gdpva.dta, replace
replace country ="China, Hong Kong SAR" if cid==44
replace country ="Indonesia" if cid==100
replace country ="Japan" if cid==108
replace country ="Korea, Republic of" if cid==114
replace country ="Malaysia" if cid==127
replace country ="Pakistan" if cid==157
replace country ="Philippines" if cid==165
replace country ="Singapore" if cid==187
replace country ="Sri Lanka" if cid==197
replace country ="Thailand" if cid==208
replace country ="United States" if cid==225

keep if country=="China, Hong Kong SAR" | country=="China, Hong Kong SAR" | country=="Indonesia" | country=="Japan" | country=="Korea, Republic of" | ///
country=="Malaysia" | country=="Pakistan" | country=="Philippines" | country=="Singapore" | country=="Sri Lanka" | country=="Thailand" | country=="United States"


drop cid
encode country, gen(cid)
order country cid
tab component
save gdpva_1.dta, replace

tab component
encode component, gen(compid)
order country cid component compid
tab compid
keep if compid==19 | compid==11 | compid==16 | compid==7 | compid==1 | compid==3 | compid==17 | compid==10 | compid==9 | compid==8
sort country compid

recode var3 .= -999999
recode var4 .= -999999
recode var5 .= -999999
recode var6 .= -999999
recode var7 .= -999999
recode var8 .= -999999
recode var9 .= -999999
recode var10 .= -999999
recode var11 .= -999999
recode var12 .= -999999
recode var13 .= -999999
recode var14 .= -999999
recode var15 .= -999999
recode var16 .= -999999
recode var17 .= -999999
recode var18 .= -999999
recode var19 .= -999999
recode var20 .= -999999
recode var21 .= -999999
recode var22 .= -999999
recode var23 .= -999999
recode var24 .= -999999
recode var25 .= -999999
recode var26 .= -999999
recode var27 .= -999999
recode var28 .= -999999
recode var29 .= -999999
recode var30 .= -999999
recode var31 .= -999999
recode var32 .= -999999
recode var33 .= -999999
recode var34 .= -999999
recode var35 .= -999999
recode var36 .= -999999
recode var37 .= -999999
recode var38 .= -999999
recode var39 .= -999999
recode var40 .= -999999
recode var41 .= -999999
recode var42 .= -999999
recode var43 .= -999999
recode var44 .= -999999
recode var45 .= -999999
recode var46 .= -999999

tab compid, gen(dum)
local j=1
while `j'<=10{
local k=3
while `k'<=46{
qui gen c`j'_`k'=var`k'*dum`j'
local k=`k'+1
}
local j=`j'+1
}
drop var3-dum10
collapse  (sum) c1_3-c10_46, by(cid)
reshape long c1_ c2_ c3_ c4_ c5_ c6_ c7_ c8_ c9_ c10_,i(cid)

recode c1_ -999999 = .
recode c2_ -999999 = .
recode c3_ -999999 = .
recode c4_ -999999 = .
recode c5_ -999999 = .
recode c6_ -999999 = .
recode c7_ -999999 = .
recode c8_ -999999 = .
recode c9_ -999999 = .
recode c10_ -999999 = .

gen year=1967+_j
drop _j

rename c1_ va_man
rename c2_ va_con
rename c3_ va_min
rename c4_ va_oth
rename c5_ va_tra
rename c6_ va_who
rename c7_ va_agr
rename c8_ va_ind
rename c9_ va_ser
rename c10_ va_tot

gen country = "Hong Kong" if cid==238
replace country = "Indonesia" if cid==239
replace country = "Japan" if cid==240
replace country = "Korea" if cid==241
replace country = "Malaysia" if cid==242
replace country = "Pakistan" if cid==243
replace country = "Philippines" if cid==244
replace country = "Singapore" if cid==245
replace country = "Sri Lanka" if cid==246
replace country = "Thailand" if cid==247
replace country = "United States" if cid==248

order country cid year

sort country year

save gdpva_2.dta, replace


***************************************************************************************************
*************Merge ILO and UNCTAD data******************
*************Note: ILO data contains employment figures******************
***************************************************************************************************

clear
use gdpva_2.dta, replace
sort country year
merge country year using panel_2.dta

tab _merge
drop _merge
save panel_3.dta, replace


drop if isic==""
order country year isic

replace agr=iagr if agr==.
replace min=imin if min==.
replace man=iman if man==.
replace con=icon if con==.
replace who=iwho if who==.
replace tra=itra if tra==.
replace oth=ioth if oth==.
replace agrminoth=iagrminoth if agrminoth==.


replace agr=. if country=="Singapore" & year>1996
replace min=. if country=="Singapore" & year>1996
replace oth=. if country=="Singapore" & year>1996


replace min=. if country=="Sri Lanka"
replace con=. if country=="Sri Lanka"

drop iagr imin iman icon iwho itra ioth iagrminoth


gen agr_va = va_agr * 1000
gen min_va = va_min * 1000
gen man_va = va_man * 1000
gen con_va = va_con * 1000
gen who_va = va_who * 1000
gen tra_va = va_tra * 1000
gen oth_va = va_oth * 1000

gen total_va = (agr_va + min_va + man_va + con_va + who_va + tra_va + oth_va)

drop va_agr va_min va_man va_con va_who va_tra va_oth va_ind va_ser va_tot


***************************************************************************************************
*********Generate weights for sectors using VA data from UNCTAD***************
***************************************************************************************************

drop if country=="United States"
drop if year > 2013

gen w_agr = agr_va/total_va
gen w_min = min_va/total_va
gen w_man = man_va/total_va
gen w_con = con_va/total_va
gen w_who = who_va/total_va
gen w_tra = tra_va/total_va
gen w_oth = oth_va/total_va

***************************************************************************************************
*********Generate APL of sectors***************
***************************************************************************************************

gen apl_agr = agr_va/agr
gen apl_min = min_va/min
gen apl_man = man_va/man
gen apl_con = con_va/con
gen apl_who = who_va/who
gen apl_tra = tra_va/tra
gen apl_oth = oth_va/oth
gen apl_agrminoth = (agr_va + min_va + oth_va)/(agrminoth)
gen apl_mincon = (min_va + con_va)/mincon



***************************************************************************************************
*********Generate productivities of T and NT sectors***************
*********aT4 & aNT4 for Chapter 4 and aT and aNT for Chapter 5**************
*********Can use Singapore data upto 1996 for Chapter 4 as data cannot be classified from 1997 (agrminoth)*************
********Hong Needs 4 different classifications, See Li(2005) for details******************
***************************************************************************************************


sort country year 

gen aT4 = .
gen aNT4 = .

replace aT4 = (apl_agr*w_agr + apl_man*w_man)
replace aNT4 = (apl_min*w_min + apl_con*w_con + apl_who*w_who + apl_tra*w_tra + apl_oth*w_oth)


replace aT4 = (apl_agr*w_agr + apl_man*w_man) if country == "Sri Lanka"
replace aNT4 = (apl_mincon*(w_min + w_con) + apl_who*w_who + apl_tra*w_tra + apl_oth*w_oth) if country == "Sri Lanka"


gen aT = .
gen aNT = .

replace aT = (apl_agr*w_agr + apl_min*w_min + apl_man*w_man + apl_who*w_who + apl_tra*w_tra  + apl_oth*w_oth) if country == "Hong Kong"
replace aNT = (apl_con*w_con) if country == "Hong Kong"

gen aT_HK1 = (apl_agr*w_agr + apl_man*w_man + apl_tra*w_tra  + apl_oth*w_oth) if country == "Hong Kong"
gen aNT_HK1 = (apl_min*w_min + apl_con*w_con + apl_who*w_who) if country == "Hong Kong"

gen aT_HK2 = (apl_agr*w_agr + apl_man*w_man + apl_min*w_min + apl_tra*w_tra  + apl_oth*w_oth) if country == "Hong Kong"
gen aNT_HK2 = (apl_con*w_con + apl_who*w_who) if country == "Hong Kong"

gen aT_HK3 = (apl_agr*w_agr + apl_man*w_man + apl_who*w_who + apl_tra*w_tra  + apl_oth*w_oth) if country == "Hong Kong"
gen aNT_HK3 = (apl_con*w_con + apl_min*w_min) if country == "Hong Kong"



replace aT = (apl_min*w_min + apl_man*w_man + apl_who*w_who + apl_tra*w_tra) if country == "Indonesia"
replace aNT = (apl_agr*w_agr + apl_con*w_con + apl_oth*w_oth) if country == "Indonesia"



replace aT = (apl_man*w_man + apl_tra*w_tra) if country == "Japan"
replace aNT = (apl_agr*w_agr + apl_min*w_min + apl_con*w_con + apl_who*w_who + apl_oth*w_oth) if country == "Japan"


replace aT = (apl_min*w_min + apl_man*w_man + apl_who*w_who + apl_tra*w_tra) if country == "Korea"
replace aNT = (apl_agr*w_agr + apl_con*w_con + apl_oth*w_oth) if country == "Korea"


replace aT = (apl_agr*w_agr + apl_min*w_min + apl_man*w_man + apl_con*w_con + apl_tra*w_tra + apl_oth*w_oth) if country == "Malaysia"
replace aNT = (apl_who*w_who) if country == "Malaysia"


replace aT = (apl_agr*w_agr + apl_min*w_min + apl_man*w_man + apl_tra*w_tra) if country == "Pakistan"
replace aNT = (apl_con*w_con + apl_who*w_who + apl_oth*w_oth) if country == "Pakistan"


replace aT = (apl_min*w_min + apl_man*w_man + apl_tra*w_tra) if country == "Philippines"
replace aNT = (apl_agr*w_agr + apl_con*w_con + apl_who*w_who + apl_oth*w_oth) if country == "Philippines"


replace aT = (apl_agrminoth*(w_agr + w_min + w_oth) + apl_man*w_man + apl_who*w_who + apl_tra*w_tra) if country == "Singapore"
replace aNT = (apl_con*w_con) if country == "Singapore"


replace aT = (apl_agr*w_agr + apl_man*w_man + apl_tra*w_tra) if country == "Sri Lanka"
replace aNT = (apl_mincon*(w_min + w_con) + apl_who*w_who + apl_oth*w_oth) if country == "Sri Lanka"


replace aT = (apl_min*w_min + apl_man*w_man) if country == "Thailand"
replace aNT = (apl_agr*w_agr + apl_con*w_con + apl_who*w_who + apl_tra*w_tra + apl_oth*w_oth) if country == "Thailand"

sort country year

save panel_4.dta, replace



***************************************************************************************************
**************Generate US productivity with ILO data*****************
***************************************************************************************************


clear
set more off
use us_emp.dta, replace


***************************************************************************************************
**************Generate sectoral weights for US****************
********Note: Sectoral weights for US are same for both 4-sector and 7-sector classifications
***************************************************************************************************

gen w_agr_is = agr_is/tot_is
gen w_min_is = min_is/tot_is
gen w_man_is = man_is/tot_is
gen w_con_is = con_is/tot_is
gen w_who_is = who_is/tot_is
gen w_tra_is = tra_is/tot_is
gen w_oth_is = oth_is/tot_is


gen w_agr_na = agr_na/tot_na
gen w_min_na = min_na/tot_na
gen w_man_na = man_na/tot_na
gen w_con_na = con_na/tot_na
gen w_who_na = who_na/tot_na
gen w_tra_na = tra_na/tot_na
gen w_oth_na = oth_na/tot_na


***************************************************************************************************
*************Generate predicted weights for ISIC********************
***************************************************************************************************

reg w_agr_is w_agr_na
predict w_agr_is_hat

reg w_min_is w_min_na
predict w_min_is_hat

reg w_man_is w_man_na
predict w_man_is_hat

reg w_con_is w_con_na
predict w_con_is_hat

reg w_who_is w_who_na
predict w_who_is_hat

reg w_oth_is w_oth_na
predict w_oth_is_hat


***************************************************************************************************
***Generate predicted values of sectors with predicted weights in ISIC with NAICS values******
***************************************************************************************************


gen new_agr_is = w_agr_is_hat * tot_na
gen new_min_is = w_min_is_hat * tot_na
gen new_man_is = w_man_is_hat * tot_na
gen new_con_is = w_con_is_hat * tot_na
gen new_who_is = w_who_is_hat * tot_na
gen new_oth_is = w_oth_is_hat * tot_na
gen new_tra_is = (1 - w_agr_is_hat - w_min_is_hat - w_man_is_hat - w_con_is_hat - w_who_is_hat - w_oth_is_hat) * tot_na

gen new_tot_is = new_agr_is + new_min_is + new_man_is + new_con_is + new_who_is + new_tra_is + new_oth_is


***************************************************************************************************
***Generate graphs to compare the best fit************
***************************************************************************************************


tsset year
tsline tot_is new_tot_is tot_na, lpattern(dash)
tsline agr_is new_agr_is agr_na, lpattern(dash)
tsline min_is new_min_is min_na, lpattern(dash)
tsline man_is new_man_is man_na, lpattern(dash)
tsline con_is new_con_is con_na, lpattern(dash)
tsline who_is new_who_is who_na, lpattern(dash)
tsline tra_is new_tra_is tra_na, lpattern(dash)
tsline oth_is new_oth_is oth_na, lpattern(dash)


save us_emp_1.dta, replace

replace agr_is = new_agr_is if year > 2002
replace min_is = new_min_is if year > 2002
replace man_is = new_man_is if year > 2002
replace con_is = new_con_is if year > 2002
replace who_is = new_who_is if year > 2002
replace tra_is = new_tra_is if year > 2002
replace oth_is = new_oth_is if year > 2002

keep year agr_is min_is man_is con_is who_is tra_is oth_is

gen tot_is = agr_is + min_is + man_is + con_is + who_is + tra_is + oth_is

gen country = "United States"

order country year
sort country year

save us_emp_2.dta, replace

clear
use panel_3.dta, replace
keep if country == "United States"
keep country cid year va_agr va_min va_man va_con va_who va_tra va_oth agr min man con who tra oth

sort country year

merge country year using us_emp_2.dta

replace agr = agr_is if year > 2002
replace min = min_is if year > 2002
replace man = man_is if year > 2002
replace con = con_is if year > 2002
replace who = who_is if year > 2002
replace tra = tra_is if year > 2002
replace oth = oth_is if year > 2002

keep country cid year va_agr va_min va_man va_con va_who va_tra va_oth agr min man con who tra oth


gen agr_va = va_agr * 1000
gen min_va = va_min * 1000
gen man_va = va_man * 1000
gen con_va = va_con * 1000
gen who_va = va_who * 1000
gen tra_va = va_tra * 1000
gen oth_va = va_oth * 1000

gen total_va = (agr_va + min_va + man_va + con_va + who_va + tra_va + oth_va)

drop va_agr va_min va_man va_con va_who va_tra va_oth


***************************************************************************************************
*********Generate weights for sectors***************
***************************************************************************************************

gen w_agr = agr_va/total_va
gen w_min = min_va/total_va
gen w_man = man_va/total_va
gen w_con = con_va/total_va
gen w_who = who_va/total_va
gen w_tra = tra_va/total_va
gen w_oth = oth_va/total_va


***************************************************************************************************
*********Generate APL of sectors***************
***************************************************************************************************

gen apl_agr = agr_va/agr
gen apl_min = min_va/min
gen apl_man = man_va/man
gen apl_con = con_va/con
gen apl_who = who_va/who
gen apl_tra = tra_va/tra
gen apl_oth = oth_va/oth


***************************************************************************************************
*********Generate productivities of T and NT sectors***************
***************************************************************************************************


gen aT_us = .
gen aNT_us = .

replace aT_us = (apl_agr*w_agr + apl_man*w_man)
replace aNT_us = (apl_min*w_min + apl_con*w_con + apl_who*w_who + apl_tra*w_tra + apl_oth*w_oth)

gen lnaT_us = ln(aT_us)
gen lnaNT_us = ln(aNT_us)
gen lna_us = lnaT_us - lnaNT_us

save us_emp_3.dta, replace

keep year aT_us aNT_us lnaT_us lnaNT_us lna_us
sort year

save us_emp_3.dta, replace


***************************************************************************************************
****************Generate Exchange Rate, CPI and GDP Deflator variables (Overall - not sectoral)*******************
***************************************************************************************************

clear
set more off
use er.dta, replace

encode country, gen(cid)
tsset cid year

gen cpi2010_05 = cpi2010 if year == 2005
egen cpi2005_05a = max(cpi2010_05), by (cid)
gen cpi2005new = (cpi2010/cpi2005_05a)*100
replace cpi2005new = cpi2005 if cpi2005new == .
drop cpi2010_05 cpi2005_05a

gen cpius2010_05 = cpius2010 if year == 2005
egen cpius2005_05a = max(cpius2010_05), by (cid)
gen cpius2005new = (cpius2010/cpius2005_05a)*100
replace cpius2005new = cpius2005 if cpius2005new == .
drop cpius2010_05 cpius2005_05a

gen gdpdef = (gdpcurrentlcu/gdpconstantlcu)*100

gen gdpdefus = (usgdpcurrlcu/usgdpconstlcu)*100

gen lnrer_cpi = ln(ner*cpius2005new/cpi2005new)
gen lnrer_def = ln(ner*gdpdefus/gdpdef)

save er1.dta, replace

keep country year ner cpi2005new cpius2005new gdpdef gdpdefus lnrer_cpi lnrer_def

sort country year

save er2.dta, replace

***************************************************************************************************
******************Generate Sectoral Price data*****************************
***************************************************************************************************

clear
set more off

use gdpvacur.dta, replace

replace country ="China, Hong Kong SAR" if cid==44
replace country ="Indonesia" if cid==100
replace country ="Indonesia2002" if cid==101
replace country ="Japan" if cid==108
replace country ="Korea, Republic of" if cid==114
replace country ="Malaysia" if cid==127
replace country ="Pakistan" if cid==157
replace country ="Philippines" if cid==165
replace country ="Singapore" if cid==187
replace country ="Sri Lanka" if cid==197
replace country ="Thailand" if cid==208
replace country ="United States" if cid==225

keep if country=="China, Hong Kong SAR" | country=="China, Hong Kong SAR" | country=="Indonesia" | country=="Japan" | country=="Korea, Republic of" | ///
country=="Malaysia" | country=="Pakistan" | country=="Philippines" | country=="Singapore" | country=="Sri Lanka" | country=="Thailand" | country=="United States"


drop cid
encode country, gen(cid)
order country cid
tab component
save gdpvacur_1.dta, replace

tab component
encode component, gen(compid)
order country cid component compid
tab compid
keep if compid==19 | compid==11 | compid==16 | compid==7 | compid==1 | compid==3 | compid==17 | compid==10 | compid==9 | compid==8
sort country compid

recode var3 .= -999999
recode var4 .= -999999
recode var5 .= -999999
recode var6 .= -999999
recode var7 .= -999999
recode var8 .= -999999
recode var9 .= -999999
recode var10 .= -999999
recode var11 .= -999999
recode var12 .= -999999
recode var13 .= -999999
recode var14 .= -999999
recode var15 .= -999999
recode var16 .= -999999
recode var17 .= -999999
recode var18 .= -999999
recode var19 .= -999999
recode var20 .= -999999
recode var21 .= -999999
recode var22 .= -999999
recode var23 .= -999999
recode var24 .= -999999
recode var25 .= -999999
recode var26 .= -999999
recode var27 .= -999999
recode var28 .= -999999
recode var29 .= -999999
recode var30 .= -999999
recode var31 .= -999999
recode var32 .= -999999
recode var33 .= -999999
recode var34 .= -999999
recode var35 .= -999999
recode var36 .= -999999
recode var37 .= -999999
recode var38 .= -999999
recode var39 .= -999999
recode var40 .= -999999
recode var41 .= -999999
recode var42 .= -999999
recode var43 .= -999999
recode var44 .= -999999
recode var45 .= -999999
recode var46 .= -999999

tab compid, gen(dum)
local j=1
while `j'<=10{
local k=3
while `k'<=46{
qui gen c`j'_`k'=var`k'*dum`j'
local k=`k'+1
}
local j=`j'+1
}
drop var3-dum10
collapse  (sum) c1_3-c10_46, by(cid)
reshape long c1_ c2_ c3_ c4_ c5_ c6_ c7_ c8_ c9_ c10_,i(cid)

recode c1_ -999999 = .
recode c2_ -999999 = .
recode c3_ -999999 = .
recode c4_ -999999 = .
recode c5_ -999999 = .
recode c6_ -999999 = .
recode c7_ -999999 = .
recode c8_ -999999 = .
recode c9_ -999999 = .
recode c10_ -999999 = .

gen year=1967+_j
drop _j

rename c1_ va_man_cur
rename c2_ va_con_cur
rename c3_ va_min_cur
rename c4_ va_oth_cur
rename c5_ va_tra_cur
rename c6_ va_who_cur
rename c7_ va_agr_cur
rename c8_ va_ind_cur
rename c9_ va_ser_cur
rename c10_ va_tot_cur

gen country = "Hong Kong" if cid==238
replace country = "Indonesia" if cid==239
replace country = "Japan" if cid==240
replace country = "Korea" if cid==241
replace country = "Malaysia" if cid==242
replace country = "Pakistan" if cid==243
replace country = "Philippines" if cid==244
replace country = "Singapore" if cid==245
replace country = "Sri Lanka" if cid==246
replace country = "Thailand" if cid==247
replace country = "United States" if cid==248

order country cid year

sort country year

save gdpvacur_2.dta, replace

clear

use er.dta, replace

keep country year ner

sort country year

save er_adj.dta, replace

clear

use gdpva_2.dta, replace

sort country year
merge country year using gdpvacur_2.dta er_adj.dta
drop _merge _merge1 _merge2

tsset cid year

gen ner_2005 = ner if year == 2005
egen ner_2005a = max(ner_2005), by (cid)

gen neradj = ner/ner_2005a
gen lnner = ln(ner)
gen lnneradj = ln(neradj)

gen va_agr_cur_lcu = va_agr_cur*ner
gen va_min_cur_lcu = va_min_cur*ner
gen va_man_cur_lcu = va_man_cur*ner
gen va_con_cur_lcu = va_con_cur*ner
gen va_who_cur_lcu = va_who_cur*ner
gen va_tra_cur_lcu = va_tra_cur*ner
gen va_oth_cur_lcu = va_oth_cur*ner
gen va_tot_cur_lcu = va_tot_cur*ner

gen va_agr_con_lcu = va_agr*ner_2005a
gen va_min_con_lcu = va_min*ner_2005a
gen va_man_con_lcu = va_man*ner_2005a
gen va_con_con_lcu = va_con*ner_2005a
gen va_who_con_lcu = va_who*ner_2005a
gen va_tra_con_lcu = va_tra*ner_2005a
gen va_oth_con_lcu = va_oth*ner_2005a
gen va_tot_con_lcu = va_tot*ner_2005a

gen p_agr_lcu = (va_agr_cur_lcu/va_agr_con_lcu)*100
gen p_min_lcu = (va_min_cur_lcu/va_min_con_lcu)*100
gen p_man_lcu = (va_man_cur_lcu/va_man_con_lcu)*100
gen p_con_lcu = (va_con_cur_lcu/va_con_con_lcu)*100
gen p_who_lcu = (va_who_cur_lcu/va_who_con_lcu)*100
gen p_tra_lcu = (va_tra_cur_lcu/va_tra_con_lcu)*100
gen p_oth_lcu = (va_oth_cur_lcu/va_oth_con_lcu)*100
gen p_tot_lcu = (va_tot_cur_lcu/va_tot_con_lcu)*100

gen p_agr = (va_agr_cur/va_agr)*100
gen p_min = (va_min_cur/va_min)*100
gen p_man = (va_man_cur/va_man)*100
gen p_con = (va_con_cur/va_con)*100
gen p_who = (va_who_cur/va_who)*100
gen p_tra = (va_tra_cur/va_tra)*100
gen p_oth = (va_oth_cur/va_oth)*100
gen p_tot = (va_tot_cur/va_tot)*100

sort country year

save gdpvacur_3.dta, replace



***************************************************************************************************
******************Generating pT and pNT (including pTlcu and pNTlcu) with sectoral prices and weights from previous dataset*****************************
***************Note: Creates LCU price variables for all sectors**************************
***************Note: Involves rebasing all price variables to base year 2005********************
***************************************************************************************************


clear
use panel_4.dta, replace
keep country year w_agr w_min w_man w_con w_who w_tra w_oth
sort country year 
merge country year using gdpvacur_3.dta


drop _merge

sort country year 

gen pT4 = .
gen pNT4 = .

replace pT4 = (p_agr*w_agr + p_man*w_man)
replace pNT4 = (p_min*w_min + p_con*w_con + p_who*w_who + p_tra*w_tra  + p_oth*w_oth)

gen pT = .
gen pNT = .

replace pT = (p_agr*w_agr + p_min*w_min + p_man*w_man + p_who*w_who + p_tra*w_tra  + p_oth*w_oth) if country == "Hong Kong"
replace pNT = (p_con*w_con) if country == "Hong Kong"


gen pT_HK1 = (p_agr*w_agr + p_man*w_man + p_tra*w_tra  + p_oth*w_oth) if country == "Hong Kong"
gen pNT_HK1 = (p_min*w_min + p_con*w_con + p_who*w_who) if country == "Hong Kong"


gen pT_HK2 = (p_agr*w_agr + p_man*w_man + p_min*w_min + p_tra*w_tra  + p_oth*w_oth) if country == "Hong Kong"
gen pNT_HK2 = (p_con*w_con + p_who*w_who) if country == "Hong Kong"


gen pT_HK3 = (p_agr*w_agr + p_man*w_man + p_who*w_who + p_tra*w_tra  + p_oth*w_oth) if country == "Hong Kong"
gen pNT_HK3 = (p_con*w_con + p_min*w_min) if country == "Hong Kong"


replace pT = (p_min*w_min + p_man*w_man + p_who*w_who + p_tra*w_tra) if country == "Indonesia"
replace pNT = (p_agr*w_agr + p_con*w_con + p_oth*w_oth) if country == "Indonesia"


replace pT = (p_man*w_man + p_tra*w_tra) if country == "Japan"
replace pNT = (p_agr*w_agr + p_min*w_min + p_con*w_con + p_who*w_who + p_oth*w_oth) if country == "Japan"


replace pT = (p_min*w_min + p_man*w_man + p_who*w_who + p_tra*w_tra) if country == "Korea"
replace pNT = (p_agr*w_agr + p_con*w_con + p_oth*w_oth) if country == "Korea"


replace pT = (p_agr*w_agr + p_min*w_min + p_man*w_man + p_con*w_con + p_tra*w_tra + p_oth*w_oth) if country == "Malaysia"
replace pNT = (p_who*w_who) if country == "Malaysia"


replace pT = (p_agr*w_agr + p_min*w_min + p_man*w_man + p_tra*w_tra) if country == "Pakistan"
replace pNT = (p_con*w_con + p_who*w_who + p_oth*w_oth) if country == "Pakistan"


replace pT = (p_min*w_min + p_man*w_man + p_tra*w_tra) if country == "Philippines"
replace pNT = (p_agr*w_agr + p_con*w_con + p_who*w_who + p_oth*w_oth) if country == "Philippines"


replace pT = (p_min*w_min + p_man*w_man) if country == "Thailand"
replace pNT = (p_agr*w_agr + p_con*w_con + p_who*w_who + p_tra*w_tra + p_oth*w_oth) if country == "Thailand"


replace pT = (p_agr*w_agr + p_min*w_min + p_oth*w_oth + p_man*w_man + p_who*w_who + p_tra*w_tra) if country == "Singapore"
replace pNT = (p_con*w_con) if country == "Singapore"


replace pT = (p_agr*w_agr + p_man*w_man + p_tra*w_tra) if country == "Sri Lanka"
replace pNT = (p_min*w_min + p_con*w_con + p_who*w_who + p_oth*w_oth) if country == "Sri Lanka"

gen pT4lcu = .
gen pNT4lcu= .

replace pT4lcu = (p_agr_lcu*w_agr + p_man_lcu*w_man)
replace pNT4lcu = (p_min_lcu*w_min + p_con_lcu*w_con + p_who_lcu*w_who + p_tra_lcu*w_tra  + p_oth_lcu*w_oth)


gen pTlcu = .
gen pNTlcu = .

replace pTlcu = (p_agr_lcu*w_agr + p_min_lcu*w_min + p_man_lcu*w_man + p_who_lcu*w_who + p_tra_lcu*w_tra  + p_oth_lcu*w_oth) if country == "Hong Kong"
replace pNTlcu = (p_con_lcu*w_con) if country == "Hong Kong"


gen pTlcu_HK1 = (p_agr_lcu*w_agr + p_man_lcu*w_man + p_tra_lcu*w_tra  + p_oth_lcu*w_oth) if country == "Hong Kong"
gen pNTlcu_HK1 = (p_min_lcu*w_min + p_con_lcu*w_con + p_who_lcu*w_who) if country == "Hong Kong"


gen pTlcu_HK2 = (p_agr_lcu*w_agr + p_man_lcu*w_man + p_min_lcu*w_min + p_tra_lcu*w_tra  + p_oth_lcu*w_oth) if country == "Hong Kong"
gen pNTlcu_HK2 = (p_con_lcu*w_con + p_who_lcu*w_who) if country == "Hong Kong"


gen pTlcu_HK3 = (p_agr_lcu*w_agr + p_man_lcu*w_man + p_who_lcu*w_who + p_tra_lcu*w_tra  + p_oth_lcu*w_oth) if country == "Hong Kong"
gen pNTlcu_HK3 = (p_con_lcu*w_con + p_min_lcu*w_min) if country == "Hong Kong"


replace pTlcu = (p_min_lcu*w_min + p_man_lcu*w_man + p_who_lcu*w_who + p_tra_lcu*w_tra) if country == "Indonesia"
replace pNTlcu = (p_agr_lcu*w_agr + p_con_lcu*w_con + p_oth_lcu*w_oth) if country == "Indonesia"


replace pTlcu = (p_man_lcu*w_man + p_tra_lcu*w_tra) if country == "Japan"
replace pNTlcu = (p_agr_lcu*w_agr + p_min_lcu*w_min + p_con_lcu*w_con + p_who_lcu*w_who + p_oth_lcu*w_oth) if country == "Japan"


replace pTlcu = (p_min_lcu*w_min + p_man_lcu*w_man + p_who_lcu*w_who + p_tra_lcu*w_tra) if country == "Korea"
replace pNTlcu = (p_agr_lcu*w_agr + p_con_lcu*w_con + p_oth_lcu*w_oth) if country == "Korea"


replace pTlcu = (p_agr_lcu*w_agr + p_min_lcu*w_min + p_man_lcu*w_man + p_con_lcu*w_con + p_tra_lcu*w_tra + p_oth_lcu*w_oth) if country == "Malaysia"
replace pNTlcu = (p_who_lcu*w_who) if country == "Malaysia"


replace pTlcu = (p_agr_lcu*w_agr + p_min_lcu*w_min + p_man_lcu*w_man + p_tra_lcu*w_tra) if country == "Pakistan"
replace pNTlcu = (p_con_lcu*w_con + p_who_lcu*w_who + p_oth_lcu*w_oth) if country == "Pakistan"


replace pTlcu = (p_min_lcu*w_min + p_man_lcu*w_man + p_tra_lcu*w_tra) if country == "Philippines"
replace pNTlcu = (p_agr_lcu*w_agr + p_con_lcu*w_con + p_who_lcu*w_who + p_oth_lcu*w_oth) if country == "Philippines"


replace pTlcu = (p_min_lcu*w_min + p_man_lcu*w_man) if country == "Thailand"
replace pNTlcu = (p_agr_lcu*w_agr + p_con_lcu*w_con + p_who_lcu*w_who + p_tra_lcu*w_tra + p_oth_lcu*w_oth) if country == "Thailand"


replace pTlcu = (p_agr_lcu*w_agr + p_min_lcu*w_min + p_oth_lcu*w_oth + p_man_lcu*w_man + p_who_lcu*w_who + p_tra_lcu*w_tra) if country == "Singapore"
replace pNTlcu = (p_con_lcu*w_con) if country == "Singapore"


replace pTlcu = (p_agr_lcu*w_agr + p_man_lcu*w_man + p_tra_lcu*w_tra) if country == "Sri Lanka"
replace pNTlcu = (p_min_lcu*w_min + p_con_lcu*w_con + p_who_lcu*w_who + p_oth_lcu*w_oth) if country == "Sri Lanka"

tsset cid year

foreach i in pT4 pNT4 pT pNT pT4lcu pNT4lcu pTlcu pNTlcu pTlcu_HK1 pNTlcu_HK1 pTlcu_HK2 pNTlcu_HK2 pTlcu_HK3 pNTlcu_HK3{
gen `i'_2005 = `i' if year == 2005
egen `i'_2005a = max(`i'_2005), by (cid)
gen `i'adj = (`i'/`i'_2005a)*100
}

sort country year
save secprice.dta, replace



***************************************************************************************************
********Create Sectoral prices for US****************
********Note: Involves rebasing prices to base year 2005***************
***************************************************************************************************



clear 
use us_emp_3.dta, replace
sort year
save us_emp_3.dta, replace


clear
use secprice.dta, replace
keep if country=="United States"
drop w_agr w_min w_man w_con w_who w_tra w_oth

gen total_va = va_agr + va_min + va_man + va_con + va_who + va_tra + va_oth

gen w_agr = va_agr/total_va
gen w_min = va_min/total_va
gen w_man = va_man/total_va
gen w_con = va_con/total_va
gen w_who = va_who/total_va
gen w_tra = va_tra/total_va
gen w_oth = va_oth/total_va

keep year w_agr w_min w_man w_con w_who w_tra w_oth p_agr p_min p_man p_con p_who p_tra p_oth
sort year
merge year using us_emp_3.dta
drop _merge

gen pT_us = .
gen pNT_us = .

replace pT_us = (p_agr*w_agr + p_man*w_man)
replace pNT_us = (p_min*w_min + p_con*w_con + p_who*w_who + p_tra*w_tra + p_oth*w_oth)

foreach i in pT_us pNT_us {
gen `i'_2005 = `i' if year == 2005
egen `i'_2005a = max(`i'_2005)
gen `i'adj = (`i'/`i'_2005a)*100
}

gen lnpT_us = ln(pT_us)
gen lnpNT_us = ln(pNT_us)

gen lnpT_usadj = ln(pT_usadj)
gen lnpNT_usadj = ln(pNT_usadj)

keep year p_agr p_min p_man p_con p_who p_tra p_oth pT_us pNT_us lnpT_us lnpNT_us pT_usadj pNT_usadj lnpT_usadj lnpNT_usadj
order year
sort year
save secprice_us.dta, replace



***************************************************************************************************
********************Merging sectoral price data of all countries with US****************
***************************************************************************************************


clear
use secprice.dta, replace
sort year
merge year using secprice_us.dta
drop _merge
drop if country=="United States"
order country cid year
sort country year
save secprice_1.dta, replace



***************************************************************************************************
*********************Merge Sectoral data with previous data and ER, CPI and GDP Deflator********************
***************************************************************************************************


clear
use panel_4.dta, replace
sort country year
merge country year using secprice_1.dta er2.dta
drop _merge _merge1 _merge2
sort year
save panel_5.dta, replace



***************************************************************************************************
****************Merge previous data with US productivity data*******************
***************************************************************************************************


clear
use panel_5.dta, replace
sort year
merge year using us_emp_3.dta
drop _merge
order country cid year
sort country year

save panel_6.dta, replace


***************************************************************************************************
***********************Generate final productivity variables for regression*********************
***********Note: Involves interpolation*********************
***************************************************************************************************


sort country year

by country: ipolate aT4 year, gen(iaT4)
replace aT4=iaT4 if aT4==.

by country: ipolate aNT4 year, gen(iaNT4)
replace aNT4=iaNT4 if aNT4==.

gen lnaT4 = ln(aT4)
gen lnaNT4 = ln(aNT4)


by country: ipolate aT year, gen(iaT)
replace aT=iaT if aT==.

by country: ipolate aNT year, gen(iaNT)
replace aNT=iaNT if aNT==.

gen lnaT = ln(aT)
gen lnaNT = ln(aNT)

gen au4 = - ((lnaT4 - lnaNT4) - (lnaT_us - lnaNT_us))
gen au = - ((lnaT - lnaNT) - (lnaT_us - lnaNT_us))
gen aTNT = lnaT - lnaNT


*******************************************************
**********For Hong Kong**************************
********************************************************

foreach i in aT_HK1 aNT_HK1 aT_HK2 aNT_HK2 aT_HK3 aNT_HK3{
gen ln`i' = ln(`i')
}


gen au_HK1 = - ((lnaT_HK1 - lnaNT_HK1) - (lnaT_us - lnaNT_us))
gen au_HK2 = - ((lnaT_HK2 - lnaNT_HK2) - (lnaT_us - lnaNT_us))
gen au_HK3 = - ((lnaT_HK3 - lnaNT_HK3) - (lnaT_us - lnaNT_us))

gen aTNT_HK1 = lnaT_HK1 - lnaNT_HK1
gen aTNT_HK2 = lnaT_HK2 - lnaNT_HK2
gen aTNT_HK3 = lnaT_HK3 - lnaNT_HK3





***************************************************************************************************
***********************Generate final sectoral price variables for regression*********************
**************Note: Involves interpolation***********************
***************************************************************************************************

*******************************************
************For Chapter 5****************
*******************************************

by country: ipolate pT4 year, gen(ipT4)
replace pT4=ipT4 if pT4==.

by country: ipolate pNT4 year, gen(ipNT4)
replace pNT4=ipNT4 if pNT4==.

gen lnpT4 = ln(pT4)
gen lnpNT4 = ln(pNT4)


by country: ipolate pT4adj year, gen(ipT4adj)
replace pT4adj=ipT4adj if pT4adj==.

by country: ipolate pNT4adj year, gen(ipNT4adj)
replace pNT4adj=ipNT4adj if pNT4adj==.

gen lnpT4adj = ln(pT4adj)
gen lnpNT4adj = ln(pNT4adj)


by country: ipolate pT4lcu year, gen(ipT4lcu)
replace pT4lcu=ipT4lcu if pT4lcu==.

by country: ipolate pNT4lcu year, gen(ipNT4lcu)
replace pNT4lcu=ipNT4lcu if pNT4lcu==.

gen lnpT4lcu = ln(pT4lcu)
gen lnpNT4lcu = ln(pNT4lcu)


by country: ipolate pT4lcuadj year, gen(ipT4lcuadj)
replace pT4lcuadj=ipT4lcuadj if pT4lcuadj==.

by country: ipolate pNT4lcuadj year, gen(ipNT4lcuadj)
replace pNT4lcuadj=ipNT4lcuadj if pNT4lcuadj==.

gen lnpT4lcuadj = ln(pT4lcuadj)
gen lnpNT4lcuadj = ln(pNT4lcuadj)




**************************************************
************For Chapter 6 and beyond****************
*************************************************


by country: ipolate pT year, gen(ipT)
replace pT=ipT if pT==.

by country: ipolate pNT year, gen(ipNT)
replace pNT=ipNT if pNT==.

gen lnpT = ln(pT)
gen lnpNT = ln(pNT)
gen lnpNTT = lnpNT - lnpT


by country: ipolate pTadj year, gen(ipTadj)
replace pTadj=ipTadj if pTadj==.

by country: ipolate pNTadj year, gen(ipNTadj)
replace pNTadj=ipNTadj if pNTadj==.

gen lnpTadj = ln(pTadj)
gen lnpNTadj = ln(pNTadj)
gen lnpNTTadj = lnpNTadj - lnpTadj


by country: ipolate pTlcu year, gen(ipTlcu)
replace pTlcu=ipTlcu if pTlcu==.

by country: ipolate pNTlcu year, gen(ipNTlcu)
replace pNTlcu=ipNTlcu if pNTlcu==.

gen lnpTlcu = ln(pTlcu)
gen lnpNTlcu = ln(pNTlcu)
gen lnpNTTlcu = lnpNTlcu - lnpTlcu


by country: ipolate pTlcuadj year, gen(ipTlcuadj)
replace pTlcuadj=ipTlcuadj if pTlcuadj==.

by country: ipolate pNTlcuadj year, gen(ipNTlcuadj)
replace pNTlcuadj=ipNTlcuadj if pNTlcuadj==.



****************************************************************************************
***********************Generate seperate price variables for Hong kong****************
****************************************************************************************


by country: ipolate pTlcu_HK1adj year, gen(ipTlcu_HK1adj)
replace pTlcu_HK1adj=ipTlcu_HK1adj if pTlcu_HK1adj==.

by country: ipolate pNTlcu_HK1adj year, gen(ipNTlcu_HK1adj)
replace pNTlcu_HK1adj=ipNTlcu_HK1adj if pNTlcu_HK1adj==.

by country: ipolate pTlcu_HK2adj year, gen(ipTlcu_HK2adj)
replace pTlcu_HK2adj=ipTlcu_HK2adj if pTlcu_HK2adj==.

by country: ipolate pNTlcu_HK2adj year, gen(ipNTlcu_HK2adj)
replace pNTlcu_HK2adj=ipNTlcu_HK2adj if pNTlcu_HK2adj==.

by country: ipolate pTlcu_HK3adj year, gen(ipTlcu_HK3adj)
replace pTlcu_HK3adj=ipTlcu_HK3adj if pTlcu_HK3adj==.

by country: ipolate pNTlcu_HK3adj year, gen(ipNTlcu_HK3adj)
replace pNTlcu_HK3adj=ipNTlcu_HK3adj if pNTlcu_HK3adj==.


gen lnpTlcuadj = ln(pTlcuadj)
gen lnpNTlcuadj = ln(pNTlcuadj)
gen lnpNTTlcuadj = lnpNTlcuadj - lnpTlcuadj

gen lnPPPNT = lnpNT_usadj - lnpNTlcuadj
gen lnPPPT = lnpT_usadj - lnpTlcuadj


foreach i in pTlcu_HK1adj pNTlcu_HK1adj pTlcu_HK2adj pNTlcu_HK2adj pTlcu_HK3adj pNTlcu_HK3adj{
gen ln`i' = ln(`i')
}


gen lnpNTTHK1 = lnpNTlcu_HK1adj - lnpTlcu_HK1adj
gen lnpNTTHK2 = lnpNTlcu_HK2adj - lnpTlcu_HK2adj
gen lnpNTTHK3 = lnpNTlcu_HK3adj - lnpTlcu_HK3adj

gen lnPPPNT_HK1 = lnpNT_usadj - lnpNTlcu_HK1adj
gen lnPPPNT_HK2 = lnpNT_usadj - lnpNTlcu_HK2adj
gen lnPPPNT_HK3 = lnpNT_usadj - lnpNTlcu_HK3adj

gen lnPPPT_HK1 = lnpT_usadj - lnpTlcu_HK1adj
gen lnPPPT_HK2 = lnpT_usadj - lnpTlcu_HK2adj
gen lnPPPT_HK3 = lnpT_usadj - lnpTlcu_HK3adj


***************************************************************************************************
***********************Generate final sectoral price based RER variables for regression*********************
***************************************************************************************************



gen lnrer_pNT = ln(ner*pNT_us/pNT)

gen lnrer_pNTadj = ln(ner*pNT_usadj/pNTadj)

gen lnrer_pNTlcu = ln(ner*pNT_us/pNTlcu)

gen lnrer_pNTlcuadj = ln(ner*pNT_usadj/pNTlcuadj)

gen lnrer_pNT4lcu = ln(ner*pNT_usadj/pNT4lcu)

gen lnrer_pNT4lcuadj = ln(ner*pNT_usadj/pNT4lcuadj)

gen lnrerPPPT = lnner + lnPPPT

gen lnrerPPPTadj = lnneradj + lnPPPT

gen pT_usPPP = lnner + lnpT_usadj


********************************************************************
*****************Specific variables for Hong Kong********************
********************************************************************

gen lnrer_pNTlcu_HK1adj = ln(ner*pNT_usadj/pNTlcu_HK1adj)
gen lnrer_pNTlcu_HK2adj = ln(ner*pNT_usadj/pNTlcu_HK2adj)
gen lnrer_pNTlcu_HK3adj = ln(ner*pNT_usadj/pNTlcu_HK3adj)


gen lnrerPPPT_HK1 = lnner + lnPPPT_HK1
gen lnrerPPPT_HK2 = lnner + lnPPPT_HK2
gen lnrerPPPT_HK3 = lnner + lnPPPT_HK3

gen lnrerPPPTadj_HK1 = lnneradj + lnPPPT_HK1
gen lnrerPPPTadj_HK2 = lnneradj + lnPPPT_HK2
gen lnrerPPPTadj_HK3 = lnneradj + lnPPPT_HK3


keep country cid year ner ner_2005a neradj lnner lnneradj cpi2005new cpius2005new gdpdef gdpdefus lnrer_cpi lnrer_def lnrer_pNT lnrer_pNTadj lnrer_pNTlcu lnrer_pNTlcuadj ///
lnrer_pNT4lcu lnrer_pNT4lcuadj aT4 aNT4 aTNT lnaT4 lnaNT4 au4 aT aNT lnaT lnaNT aT_us aNT_us lnaT_us lnaNT_us au pT4 pNT4 pT4lcu pNT4lcu lnpT4 lnpNT4 lnpT4lcu lnpNT4lcu ///
pT pNT pTlcu pNTlcu lnpT lnpNT lnpNTT lnpTlcu lnpNTlcu lnpNTTlcu pT_us pNT_us lnpT_us lnpNT_us pT_HK1 pNT_HK1 pT_HK2 pNT_HK2 pT_HK3 pNT_HK3 pTlcu_HK1 pNTlcu_HK1 pTlcu_HK2 ///
pNTlcu_HK2 pTlcu_HK3 pNTlcu_HK3 pT4adj pNT4adj pTadj pNTadj pT4lcuadj pNT4lcuadj pTlcuadj pNTlcuadj pTlcu_HK1adj pNTlcu_HK1adj pTlcu_HK2adj pNTlcu_HK2adj pTlcu_HK3adj ///
pNTlcu_HK3adj pT_usadj pNT_usadj lnpT_usadj lnpNT_usadj lnpT4adj lnpNT4adj lnpTadj lnpNTadj lnpNTTadj lnpT4lcuadj lnpNT4lcuadj lnpTlcuadj lnpNTlcuadj lnpNTTlcuadj ///
lnPPPNT lnPPPT lnrerPPPT lnrerPPPTadj pT_usPPP lnpNTTHK1 lnpNTTHK2 lnpNTTHK3 lnPPPNT_HK1 lnPPPNT_HK2 lnPPPNT_HK3 lnPPPT_HK1 lnPPPT_HK2 lnPPPT_HK3 ///
lnrer_pNTlcu_HK1adj lnrer_pNTlcu_HK2adj lnrer_pNTlcu_HK3adj lnrerPPPT_HK1 lnrerPPPT_HK2 lnrerPPPT_HK3 lnrerPPPTadj_HK1 lnrerPPPTadj_HK2 lnrerPPPTadj_HK3 ///
au_HK1 au_HK2 au_HK3 aTNT_HK1 aTNT_HK2 aTNT_HK3 lnrerPPPT lnrerPPPTadj pT_usPPP lnaT_HK1 lnaNT_HK1 lnaT_HK2 lnaNT_HK2 lnaT_HK3 lnaNT_HK3 ///
lnpNTlcu_HK1adj lnpTlcu_HK1adj lnpNTlcu_HK2adj lnpTlcu_HK2adj lnpNTlcu_HK3adj lnpTlcu_HK3adj


order country cid year ner ner_2005a neradj lnner lnneradj lnrer_cpi lnrer_def lnrer_pNT lnrer_pNTadj lnrer_pNTlcu lnrer_pNTlcuadj lnrer_pNT4lcu lnrer_pNT4lcuadj cpi2005new ///
cpius2005new gdpdef gdpdefus pT4 pNT4 lnpT4 lnpNT4 pT4lcu pNT4lcu pT pNT pTlcu pNTlcu lnpT lnpNT lnpNTT lnpT4lcu lnpNT4lcu lnpTlcu lnpNTlcu lnpNTTlcu pT_us pNT_us lnpT_us ///
lnpNT_us pT_HK1 pNT_HK1 pT_HK2 pNT_HK2 pTlcu_HK1 pNTlcu_HK1 pTlcu_HK2 pNTlcu_HK2 pT_HK3 pNT_HK3 pTlcu_HK3 pNTlcu_HK3  aT4 aNT4 lnaT4 lnaNT4 au4 aT aNT aTNT lnaT lnaNT ///
aT_us aNT_us lnaT_us lnaNT_us au pT4adj pNT4adj pTadj pNTadj pT4lcuadj pNT4lcuadj pTlcuadj pNTlcuadj pTlcu_HK1adj pNTlcu_HK1adj pTlcu_HK2adj pNTlcu_HK2adj pTlcu_HK3adj ///
pNTlcu_HK3adj pT_usadj pNT_usadj lnpT_usadj lnpNT_usadj lnpT4adj lnpNT4adj lnpTadj lnpNTadj lnpNTTadj lnpT4lcuadj lnpNT4lcuadj lnpTlcuadj lnpNTlcuadj lnpNTTlcuadj ///
lnPPPNT lnPPPT lnrerPPPT lnrerPPPTadj pT_usPPP lnpNTTHK1 lnpNTTHK2 lnpNTTHK3 lnPPPNT_HK1 lnPPPNT_HK2 lnPPPNT_HK3 lnPPPT_HK1 lnPPPT_HK2 lnPPPT_HK3 ///
lnrer_pNTlcu_HK1adj lnrer_pNTlcu_HK2adj lnrer_pNTlcu_HK3adj lnrerPPPT_HK1 lnrerPPPT_HK2 lnrerPPPT_HK3 lnrerPPPTadj_HK1 lnrerPPPTadj_HK2 lnrerPPPTadj_HK3 ///
au_HK1 au_HK2 au_HK3 aTNT_HK1 aTNT_HK2 aTNT_HK3 lnrerPPPT lnrerPPPTadj pT_usPPP lnaT_HK1 lnaNT_HK1 lnaT_HK2 lnaNT_HK2 lnaT_HK3 lnaNT_HK3 ///
lnpNTlcu_HK1adj lnpTlcu_HK1adj lnpNTlcu_HK2adj lnpTlcu_HK2adj lnpNTlcu_HK3adj lnpTlcu_HK3adj


********************************************************************************************************
*************Generate graphs for visual inspections for productivity variables and FINAL DATASET************
*********************************************************************************************************


tsset cid year
tsline lnaT lnaNT if country=="Indonesia"
tsline lnaT lnaNT if country=="Japan"
tsline lnaT lnaNT if country=="Korea"
tsline lnaT lnaNT if country=="Malaysia"
tsline lnaT lnaNT if country=="Pakistan"
tsline lnaT lnaNT if country=="Philippines"
tsline lnaT lnaNT if country=="Singapore"
tsline lnaT lnaNT if country=="Sri Lanka"

sort country year

save panel_7.dta, replace


********************************************************************************************************
*************Keep variables for regression in FINAL DATASET************
*********************************************************************************************************

clear
use panel_7.dta, replace

keep country cid year lnner lnneradj lnrer_cpi lnrer_def lnrer_pNT4lcuadj lnrer_pNTlcuadj lnrer_pNTlcu_HK1adj lnrer_pNTlcu_HK2adj lnrer_pNTlcu_HK3adj ///
lnaT lnaNT au4 au au_HK1 au_HK2 au_HK3 aTNT aTNT_HK1 aTNT_HK2 aTNT_HK3 lnpTlcuadj lnpNTlcuadj lnpNTTlcuadj lnpNTTHK1 lnpNTTHK2 lnpNTTHK3 lnPPPNT lnPPPNT_HK1 lnPPPNT_HK2 lnPPPNT_HK3 ///
lnrerPPPT lnrerPPPTadj pT_usPPP lnrerPPPT_HK1 lnrerPPPT_HK2 lnrerPPPT_HK3 lnrerPPPT lnrerPPPTadj pT_usPPP lnrerPPPTadj_HK1 lnrerPPPTadj_HK2 lnrerPPPTadj_HK3 ///
lnaT_HK1 lnaNT_HK1 lnaT_HK2 lnaNT_HK2 lnaT_HK3 lnaNT_HK3 lnpNTlcu_HK1adj lnpTlcu_HK1adj lnpNTlcu_HK2adj lnpTlcu_HK2adj lnpNTlcu_HK3adj lnpTlcu_HK3adj

order country cid year lnner lnneradj lnrer_cpi lnrer_def lnrer_pNT4lcuadj lnrer_pNTlcuadj lnrer_pNTlcu_HK1adj lnrer_pNTlcu_HK2adj lnrer_pNTlcu_HK3adj ///
lnaT lnaNT au4 au au_HK1 au_HK2 au_HK3 aTNT aTNT_HK1 aTNT_HK2 aTNT_HK3 lnpTlcuadj lnpNTlcuadj lnpNTTlcuadj lnpNTTHK1 lnpNTTHK2 lnpNTTHK3 lnPPPNT lnPPPNT_HK1 lnPPPNT_HK2 lnPPPNT_HK3 ///
lnrerPPPT lnrerPPPTadj pT_usPPP lnrerPPPT_HK1 lnrerPPPT_HK2 lnrerPPPT_HK3 lnrerPPPT lnrerPPPTadj pT_usPPP lnrerPPPTadj_HK1 lnrerPPPTadj_HK2 lnrerPPPTadj_HK3 ///
lnaT_HK1 lnaNT_HK1 lnaT_HK2 lnaNT_HK2 lnaT_HK3 lnaNT_HK3 lnpNTlcu_HK1adj lnpTlcu_HK1adj lnpNTlcu_HK2adj lnpTlcu_HK2adj lnpNTlcu_HK3adj lnpTlcu_HK3adj

rename lnner ner
rename lnneradj ner_a
rename lnrer_cpi rer_cpi
rename lnrer_def rer_def
rename lnrer_pNT4lcuadj rer_def_NT4
rename lnrer_pNTlcuadj rer_def_NT
rename lnrer_pNTlcu_HK1adj r_def_NT_HK1
rename lnrer_pNTlcu_HK2adj r_def_NT_HK2
rename lnrer_pNTlcu_HK3adj r_def_NT_HK3
rename lnpNTTlcuadj pNTT
rename lnpNTTHK1 pNTT_HK1
rename lnpNTTHK2 pNTT_HK2
rename lnpNTTHK3 pNTT_HK3
rename lnPPPNT p_NT
rename lnPPPNT_HK1 p_NT_HK1
rename lnPPPNT_HK2 p_NT_HK2
rename lnPPPNT_HK3 p_NT_HK3
rename lnrerPPPT rer_def_T
rename pT_usPPP pT_us_PPPP
rename lnrerPPPTadj rer_Ta
rename lnrerPPPT_HK1 rer_def_T_HK1
rename lnrerPPPT_HK2 rer_def_T_HK2
rename lnrerPPPT_HK3 rer_def_T_HK3
rename lnrerPPPTadj_HK1 r_T_HK1a
rename lnrerPPPTadj_HK2 r_T_HK2a
rename lnrerPPPTadj_HK3 r_T_HK3a
rename lnaT aT
rename lnaNT aNT
rename lnpTlcuadj pT
rename lnpNTlcuadj pNT
rename lnaT_HK1 aT_HK1
rename lnaNT_HK1 aNT_HK1
rename lnaT_HK2 aT_HK2
rename lnaNT_HK2 aNT_HK2
rename lnaT_HK3 aT_HK3
rename lnaNT_HK3 aNT_HK3
rename lnpNTlcu_HK1adj pNTHK1
rename lnpTlcu_HK1adj pTHK1
rename lnpNTlcu_HK2adj pNTHK2
rename lnpTlcu_HK2adj pTHK2
rename lnpNTlcu_HK3adj pNTHK3
rename lnpTlcu_HK3adj pTHK3


label var country "Country Name"
label var cid "Country ID"
label var year "Year"
label var ner "Log Nominal Exchange Rate"
label var ner_a "Log Nominal Exchange Rate (Adjusted for base year = 2005)"
label var rer_cpi "Log CPI Based Real Exchange Rate"
label var rer_def "Log GDP Deflator Based Real Exchange Rate"
label var rer_def_NT4 "Log NT Sector (4-Sector Classification) Based Real Exchange Rate"
label var rer_def_NT "Log NT Sector (7-Sector Classification) Based Real Exchange Rate"
label var rer_def_NT_HK1 "Log NT Sector (7-Sector Classification) Based Real Exchange Rate - HK1"
label var rer_def_NT_HK2 "Log NT Sector (7-Sector Classification) Based Real Exchange Rate - HK2"
label var rer_def_NT_HK3 "Log NT Sector (7-Sector Classification) Based Real Exchange Rate - HK3"
label var au4 "Log Inter-sector and Inter-Country Productivity (4-Sector Classification)"
label var au "Log Inter-sector and Inter-Country Productivity (7-Sector Classification)"
label var au_HK1 "Log Inter-sector and Inter-Country Productivity (7-Sector Classification) - HK1"
label var au_HK2 "Log Inter-sector and Inter-Country Productivity (7-Sector Classification) - HK2"
label var au_HK3 "Log Inter-sector and Inter-Country Productivity (7-Sector Classification) - HK3"
label var aTNT "Log T minus NT Productivity (Domestic)"
label var aTNT_HK1 "Log T minus NT Productivity (Domestic) - HK2"
label var aTNT_HK2 "Log T minus NT Productivity (Domestic) - HK3"
label var aTNT_HK3 "Log T minus NT Productivity (Domestic) - HK4"
label var pNTT "Log Price NT minus T (Domestic)"
label var pNTT_HK1 "Log Price NT minus T (Domestic) - HK2"
label var pNTT_HK2 "Log Price NT minus T (Domestic) - HK3"
label var pNTT_HK3 "Log Price NT minus T (Domestic) - HK4"
label var p_NT "Log Price NT (US) minus NT (Domestic)"
label var p_NT_HK1 "Log Price NT (US) minus NT (Domestic) - HK2"
label var p_NT_HK2 "Log Price NT (US) minus NT (Domestic) - HK3"
label var p_NT_HK3 "Log Price NT (US) minus NT (Domestic) - HK4"
label var rer_def_T "Log Price T (US) minus T (Domestic) (Tradable PPP)"
label var rer_Ta "Log Tradable Sector RER (Tradable PPP) Adjusted for NER (Base Year = 2005)"
label var rer_def_T_HK1 "Log Tradable Sector RER (Tradable PPP) - HK2"
label var rer_def_T_HK2 "Log Tradable Sector RER (Tradable PPP) - HK3"
label var rer_def_T_HK3 "Log Tradable Sector RER (Tradable PPP) - HK4"
label var r_T_HK1a "Log Tradable Sector RER (Tradable PPP) Adjusted for NER (Base Year = 2005) - HK2"
label var r_T_HK2a "Log Tradable Sector RER (Tradable PPP) Adjusted for NER (Base Year = 2005) - HK3"
label var r_T_HK3a "Log Tradable Sector RER (Tradable PPP) Adjusted for NER (Base Year = 2005) - HK4"
label var aT "Log T Productivity (Domestic)"
label var aNT "Log NT Productivity (Domestic)"
label var pT "Log Price T (Index)"
label var pT_us_PPP "Log NER plus log U.S. tradables prices"
label var pNT "Log Price NT (Index)"
label var aT_HK1 "Log T Productivity (Domestic) - HK2"
label var aNT_HK1 "Log NT Productivity (Domestic) - HK2"
label var aT_HK2 "Log T Productivity (Domestic) - HK3"
label var aNT_HK2 "Log NT Productivity (Domestic) - HK3"
label var aT_HK3 "Log T Productivity (Domestic) - HK4"
label var aNT_HK3 "Log NT Productivity (Domestic) - HK4"
label var pNTHK1 "Log Price NT (Domestic) - HK2"
label var pTHK1 "Log Price T (Domestic) - HK2"
label var pNTHK2 "Log Price NT (Domestic) - HK3"
label var pTHK2 "Log Price T (Domestic) - HK3"
label var pNTHK3 "Log Price NT (Domestic) - HK4"
label var pTHK3 "Log Price T (Domestic) - HK4"


sort country year

save finaldata.dta, replace



********************************************************************************************************
*************************End****************************
********************************************************************************************************
 






























